import org.omg.CORBA.PUBLIC_MEMBER;

public class MySingleList {
    static class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;//定义头节点，属于这个链表

    //头插法
    public void addFirst(int data){
ListNode node=new ListNode(data);
    node.next=head;//这两行可不能反过来写
    head=node;//插入的时候先绑定后面节点的信息
    }
    //尾插法
    public void addLast(int data){
        ListNode node=new ListNode(data);
        ListNode cur=head;
        //如果当前链表为空
        if(cur==null){
            head=node;
            return;
        }
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }
    //任意位置插入（第一个数据位置是0下标）
    public void addIndex(int index,int data){
        if(index<0||index>size()){
            System.out.println("位置不合法");
        }
        if (index==0){
            addFirst(data);
        }
        if (index==size()){
            addLast(data);
        }
        ListNode node=new ListNode(data);
ListNode cur=head;
        for (int i = 0; i < index-1; i++) {
            cur=cur.next;
        }
        node.next=cur.next;
        cur.next=node;
    }
    //查找是否包含关键字key再链表中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字key的节点
    public void remove(int key){
        if (head==null){
            return;
        }
        if (head.val==key){
            head=head.next;
        }
ListNode prev=FindPrev(key);

            ListNode node = prev.next;
            prev.next = node.next;

    }
public ListNode FindPrev(int key){
        ListNode cur=head;
        while (cur!=null){
            if (cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
}


    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (head==null){
            return;
        }
ListNode cur=head.next;
ListNode pre=head;
while (cur!=null){
    if (cur.val==key){
        pre.next=cur.next;
        cur=cur.next;
    }else {
        pre=cur;
        cur=cur.next;
    }
}
if (head.val==key){
    head=head.next;
}
    }
    //得到单链表的长度
    public int size() {
int count=0;;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;

        }
        return count;
    }
    //找到中间节点
    public ListNode midNode(){

        if (head==null){
            return null;
        }
        if (head.next==null){
            return head;
        }
       ListNode fast=head;
        ListNode slow=head;
        while (fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        return slow;
    }
//反转链表
    public ListNode reverseList(){
        if (head==null){
            return null;
        }
        if (head.next==null){
            return head;
        }
ListNode cur=head.next;
head.next=null;

while (cur!=null){
    ListNode curNext=cur.next;
    curNext=head;
    head=cur;
    cur=curNext;
}
return head;
    }


    //找到倒数第k个节点
    public ListNode findKtoTail(int k){
        ListNode fast=head;
        ListNode slow=head;
        if (head==null||k<=0) {
            return null;
        }
        for (int i = 0; i <=k-1; i++) {
            fast=fast.next;
        }
        while (fast!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
    //链表分割
public ListNode partition(int x){
        ListNode bs=null;
        ListNode be=null;
        ListNode as=null;
        ListNode ae=null;
        ListNode cur=head;
        while (cur!=null){
            if (cur.val<x){
                if (bs==null){
                    bs=be=cur;

                }else {
                    be.next=cur;
                    be=be.next;
                }


            }else {
                if (as==null){
                    as=ae=cur;

                }else {
                    ae.next=cur;
                    ae=ae.next;
                }
            }
            cur=cur.next;
        }
        if (bs==null){
            return as;
        }
        if(as!=null){
            ae.next=null;
        }
        be.next=as;
        return bs;
}

//判断链表是否回文
    public boolean chkPalindrome(){
      //先找到中间位置，然后反转后面的节点
        ListNode fast=head;
        ListNode slow=head;
        while (fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        //开始反转
        ListNode cur=slow.next;
        while (cur!=null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
            while (head != slow) {
                if (head.val != slow.val) {
                    return false;
                }
                if (head.next == slow) {
                    return true;
                }
                head = head.next;
                slow = slow.next;
            }

        }
return true;
    }
//创建一个环
    public void createLoop(){
        ListNode cur=head;
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next=head;
    }

//判断链表是否有环
    public boolean hasCycle(){
if (head==null){
    return false;
}
ListNode fast=head;
ListNode slow=head;
while (fast!=null&&fast.next!=null){ //让一个跑得快，一个跑得慢，如果可以相遇，一定有环
    fast=fast.next.next;
    slow=slow.next;
    if (fast==slow){
        return true;
    }

}
        return false;
    }
//找入环的第一个节点
    public ListNode detectCycle() {
        //通过推算可知，起点到入口的距离=相遇点到入口的距离
        if (head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) { //让一个跑得快，一个跑得慢，如果可以相遇，一定有环
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
            if (fast == null || fast.next == null) {
                return null;
            }
            fast = head;
            while (fast != slow) {
                fast = fast.next;
                slow = slow.next;
            }
            return fast;


    }
//删除链表中重复的节点
    public ListNode deleteDuplication(ListNode pHead){
//申请一个虚拟节点
ListNode newHead=new ListNode(-1);
ListNode tmp=newHead;
ListNode cur=pHead;
while (cur!=null){
    if (cur.next!=null&&cur.val==cur.next.val){
      //一直让cur走到不重复的节点
      while (cur.next!=null&&cur.val==cur.next.val) {
          cur=cur.next;
      }
       cur=cur.next;
    }else {
        tmp.next=cur;
        tmp=tmp.next;
        cur=cur.next;
    }
}

tmp.next=null;
      return newHead.next;
    }


    //清空链表
    public void clear(){
this.head=null;
    }
    //打印链表
    public void dispaly(){
ListNode cur=head;
while (cur!=null){
    System.out.print(cur.val+" ");
    cur=cur.next;
}
    }
    public void dispaly(ListNode listNode){
        ListNode cur=listNode;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }

}















